package com.example.myapplication.data.repository

import com.example.myapplication.data.model.*
import com.example.myapplication.data.network.ApiClient
import retrofit2.Response

class AuthRepository {
    private val apiService = ApiClient.apiService
    
    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @return 登录结果
     */
    suspend fun login(username: String, password: String): Result<ApiResponse<User>> {
        return try {
            val request = LoginRequest(username, password)
            val response: Response<ApiResponse<User>> = apiService.login(request)
            
            if (response.isSuccessful) {
                Result.success(response.body()!!)
            } else {
                Result.failure(Exception("登录失败: ${response.message()}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
    
    /**
     * 用户注册
     * @param username 用户名
     * @param password 密码
     * @param email 邮箱
     * @return 注册结果
     */
    suspend fun register(username: String, password: String, email: String): Result<ApiResponse<User>> {
        return try {
            val request = RegisterRequest(username, password, email)
            val response: Response<ApiResponse<User>> = apiService.register(request)
            
            if (response.isSuccessful) {
                Result.success(response.body()!!)
            } else {
                Result.failure(Exception("注册失败: ${response.message()}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}